// uni-app请求封装
export default class Request {
	http(router, data = {}, method,contentType) {
		let that = this;
		//let path = 'http://192.168.31.108:7778';
		let path = 'https://tch.frh-edu.com';  
		//let path = 'https://frh.konmeit.com';
		uni.setStorageSync('requestPath',path);
		uni.showLoading({
			title: '加载中'
		});
		return new Promise((resolve, reject) => {
			//uni.setStorageSync('AppToken',null); //手动退出
			let token = uni.getStorageSync('AppToken');
			
			var httpContentType='application/x-www-form-urlencoded';
			if(contentType!=undefined){
				//application/json;charset=UTF-8
				httpContentType=contentType;
			}
			var routers=router;
			// 请求
			uni.request({
				header: {
					// 'Content-Type': 'application/x-www-form-urlencoded',
					'Content-Type': httpContentType,
					'AppToken': token
				},
				url: `${path}${router}`,
				data: data,
				method: method,
				success: (res) => {
					//收到开发者服务器成功返回的回调函数
					console.log(res.data)
					if(res.data.code==401){//没有权限直接退出到登录界面
						uni.hideLoading();
						return;
					}
					if (res.data.token) {
						uni.setStorageSync('AppToken',res.data.token)
					}
					resolve(res.data)
				},
				fail:(res) =>{
					//接口调用失败的回调函数
				},
				complete:(res) =>{
					//接口调用结束的回调函数（调用成功、失败都会执行）
					if (res.data.code == 401) {
						uni.showToast({
							title: "请先登入！",
							icon: "error"
						});
						uni.hideLoading();
						return false
					}
					uni.hideLoading();
				}
				
			})
		})
	}
	
}
